package com.pxmeta.app.controller.user;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.google.common.collect.Maps;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.constant.UserVisitMode;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserVisit;
import com.pxmeta.user.entity.dto.FollowUserDTO;
import com.pxmeta.user.entity.dto.UnFollowUserDTO;
import com.pxmeta.user.entity.dto.UserDTO;
import com.pxmeta.user.service.IUserFollowService;
import com.pxmeta.user.service.IUserVisitService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user/follow")
@RequiredArgsConstructor
@Api(tags = "用户关注",value = "用户关注")
public class UserFollowController {

    private final IUserFollowService userFollowService;


    @SaCheckLogin
    @ApiOperation(value = "关注用户")
    @PostMapping("/followUser")
    public AjaxResult followUser(@RequestBody @Valid FollowUserDTO dto){
        Long userId = SecurityUtils.getUserId();
        if (userId.equals(dto.getFollowUserId())){
            return AjaxResult.error( HttpStatus.PARAMS_ERROR,"您不能关注自己！");
        }
        userFollowService.followUser(userId,dto.getFollowUserId(),dto.getCircleId());
        return AjaxResult.success();
    }

    @SaCheckLogin
    @ApiOperation(value = "取消关注用户")
    @ApiImplicitParam(name = "unFollowUserId",value = "取消关注用户id")
    @PutMapping("/unFollowUser")
    public AjaxResult unFollowUser(@RequestBody @Valid UnFollowUserDTO dto){
        Long userId = SecurityUtils.getUserId();
        userFollowService.unFollowUser(userId,dto.getUnFollowUserId());
        return AjaxResult.success();
    }

    @SaCheckLogin
    @ApiOperation(value = "查询是否关注")
    @GetMapping("/isFollow")
    @ApiImplicitParam(name = "targetUserId",value = "目标用户id")
    public AjaxResult isFollow(@RequestParam Long targetUserId){
        Long userId = SecurityUtils.getUserId();
        boolean follow = userFollowService.isFollow(userId, targetUserId);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("isFollow", follow);
        return AjaxResult.success(map);
    }


    @SaCheckLogin
    @GetMapping("/getFollowUsers")
    @ApiOperation(value = "获取关注用户列表")
    public AjaxResult getFollowUsers(@RequestParam Integer page,
                                     @RequestParam Integer size){
        Long userId = SecurityUtils.getUserId();
        List<UserDTO> users = userFollowService.getFollowUsers(userId, page, size);
        return AjaxResult.success(users);
    }

    @SaCheckLogin
    @GetMapping("/getFansUsers")
    @ApiOperation(value = "获取粉丝列表")
    public AjaxResult getFansUsers(@RequestParam Integer page,
                                     @RequestParam Integer size){
        Long userId = SecurityUtils.getUserId();
        List<UserDTO> users = userFollowService.getFansUsers(userId, page, size);
        return AjaxResult.success(users);
    }

    /**
     * 相互关注则为好友
     * @return
     */
    @SaCheckLogin
    @GetMapping("/getFriends")
    @ApiOperation("获取好友列表")
    public AjaxResult<List<UserDTO>> getFriends(@RequestParam Integer page,
                                             @RequestParam Integer size,
                                             @RequestParam(required = false) String keyword,
                                             @RequestParam(required = false) Long circleId){
        Long userId = SecurityUtils.getUserId();
        List<UserDTO> friendUsers = userFollowService.getFriendUsers(userId,page, size, keyword, circleId, null);
        return AjaxResult.success(friendUsers);
    }





    /**
     * 相互关注则为好友
     * @return
     */
    @SaCheckLogin
    @GetMapping("/getChatGroupInviters")
    @ApiOperation("获取群聊邀请人列表")
    public AjaxResult<List<UserDTO>> getChatGroupInviters(@RequestParam Integer page,
                                             @RequestParam Integer size,
                                             @RequestParam(required = false) String keyword,
                                                       @RequestParam Long chatGroupId){
        Long userId = SecurityUtils.getUserId();
        List<UserDTO> friendUsers = userFollowService.getChatGroupInviters(userId,page, size, keyword,chatGroupId);
        return AjaxResult.success(friendUsers);
    }

}
